<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // var str='zks';
        // console.log(str.length);

        //字符串也有indexof 和lastindexof
        var strs = '改革春风吹春满地'; //从0 开始
        // console.log(strs.indexOf('改革'));
        // console.log(strs.indexOf('春', 2)); //可以indexof ('查找的元素'，起始位置);
    </script>
    <!-- 查找字符出现次数 -->
    <script>
        function fn() {
            var str = 'abcabcabcabcabc';
            var index = str.indexOf('a');
            var sum = 0
            while (true) {
                if (index !== -1) {
                    sum++;
                    index = str.indexOf('a', index + 1)
                } else {
                    break
                }

            }
            return sum
        }
        console.log(fn());
    </script>

    <!-- 找到数组中字符次数 -->
    <script>
        var arr = ['red', 'blue', 'red', 'red'];
        var index = arr.indexOf('red')
        var sum = 0
        // console.log(index);
        while (index !== -1) {
            sum++
            index = arr.indexOf('red', index + 1)
        }
        console.log(sum);
    </script>

</head>

<body>

</body>

</html>